Chapter 13 - Working with Python files

In the previous blocks, we've mainly used notebooks to develop and run our Python code. In this chapter, we'll introduce how to create python programs (.py files) and how to run them. The most common way to work with Python is actually to use .py files, which is why it is important that you know how to work with them. You can see python files as one cell in a notebook without any markdown.

If you have any questions about this chapter, please refer to the forum on Canvas.

Before we write actual code in a .py file, we will explain the basics you need to know for doing this:

  • Chosing an editor
  • starting the terminal (from which you will run your .py files)

At the end of this chapter, you will be able to

  • create python programs in .py files
  • run python programs from the command line

1. Editor

We first need to choose which editor we will use to develop our Python code.

There are two options.

  1. You create the python programs in your browser. After opening Jupyter notebook, you can click 'New' and then 'Text file' to start developing Python programs.

  2. You install an editor. Please take a look here to get an impression of which ones are out there. We can highly recommend Atom (for macOS, Windows, Linux). Other options are BBEdit (for macOS) and Notepad++ (for Windows). A simple way to create a new .py file usually is to open a new file and save it as name_of_your_program.py (make sure to use indicative names).

Please choose between options 1 and 2.

2. Starting the terminal

To run a .py file we wrote in an editor, we need to start the terminal. This works differently for windows and Mac:

  1. On Windows, please how a look at this on how to start the terminal
  2. on OS X/MacOS (Mac computer), please type terminal in Spotlight and start the terminal

It's a useful skill to know how to navigate through your computer (i.e. go from one directory to another, all the files and subdirectories in a directory, etc) using the terminal.

For Windows users, this is a good tutorial.

For OS X/MacOS/Linux/Ubuntu users, this is a good tutorial.

3. Running your first program

Here, we'll show you how to run your first program (hello_world.py).

In the same folder as this notebook, you will find a file called hello_world.py.

Running it works differently on Windows and Mac. Below, instructions for both can be found:

A.) Running the program on OS X/MacOS

Please use the terminal to navigate to the folder in which this notebook is placed by copying the output of following cell in your terminal


In [2]:
import os
cwd = os.getcwd()
cwd_escaped_spaces = cwd.replace(' ', '\ ')
print('cd', cwd_escaped_spaces)


cd /Users/piasommerauer/Github/python-for-text-analysis/Chapters

Please run the following command in the terminal:

python hello_world.py

You've succesfully run your first Python program!

B.) Running the program on Windows

Please use the terminal to navigate to the folder in which this notebook is placed by copying the output of the following cell in your terminal


In [4]:
cwd = os.getcwd()
cwd_escaped_spaces = cwd.replace(' ', '^ ')
print('cd', cwd_escaped_spaces)


cd /Users/piasommerauer/Github/python-for-text-analysis/Chapters

Please run the output of the following command in the terminal:


In [3]:
import sys
print(sys.executable + ' hello_world.py')


/anaconda/bin/python hello_world.py

You've succesfully run your first Python program!

4. Import your own functions

In Chapter 12, you've been introduced to importing modules and functions/methods. You can see any python program that you create (so any .py file) as a module, which means that you can import it into another python program. Let's see how this works.

Please note that the following examples only work if all your python files are in the same directory. There are ways of importing python modules from other directories, but we will not discuss them here.

4.1 Importing your entire module

When importing your own functions from your own modules, several things are important. We have created two example scripts to illustrate them called the_program.py and utils.py. We recommend to open them check the following things:

  • The extension .py is not used when importing modules. import utils will import the file utils.py [line 1 the_program.py]
  • We can use any function from the file. E.g. we can call the count_words function by typing utils.count_words [the_program.py line 6]
  • We can use any global variable declared in the imported module. E.g. utils.x and utils.python declared in utils.py can be used in the_program.py

4.2 Importing functions and variables individually

We can import specific functions using the syntax from MODULE import FUNCTION/VARIABLE

This can be seen in the file the_program_v2.py (lines 1-3). (Open the files the_program_v2.py and utils.py in an editor to check this).

4.3 Importing functions and variables to python notebooks

Please note that you can also import functions and variables from a python program while using notebooks. In this case, simply treat the notebook as the python files the_program.py and the_program_v2.py.


In [ ]:
from utils import count_words

In [ ]:
words = ['how', 'often', 'does', 'each', 'string', 'occur', 'in', 'this', 'list', '?']

word2freq = count_words(words)
print('word2freq', word2freq)

Exercises

Exercise 1:

Please create and run your own program using an editor and the terminal. Please copy your beersong into your first program. Tip: simply open a new file in the editor and save it as `beersong.py'.

Exercise 2:

Please create two files:

  • my_second_program.py
  • my_utils.py

Please create a helper function and store it in my_utils.py, import it into my_second_program.py and call it from there.